package com.example.light.web.personal.service.impl;

import com.example.light.common.exception.GlobalException;
import com.example.light.common.result.CodeMsg;
import com.example.light.web.personal.dao.MessageMapper;
import com.example.light.web.personal.entity.Message;
import com.example.light.web.personal.service.MessageService;
import com.example.light.web.system.dao.UserMapper;
import com.example.light.web.system.entity.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.util.Date;
import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public void selectMessages(String userEmail, Integer page, Model model, Integer status, String search) {
        PageHelper.startPage(page, 10);
        List<Message> messages =  messageMapper.selectWithUserEmail(userEmail,status,search);
        messages.forEach(message -> {
            if(message.getMsgTitle().length() >= 10)
                message.setMsgTitle(message.getMsgTitle().substring(0,10));
            if(message.getMsgContent().length() >= 10)
            message.setMsgContent(message.getMsgContent().substring(0,10));
        });
        PageInfo<Message> pageInfo = new PageInfo<>(messages);
        searchMessageStatus(userEmail,model);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("messages",messages);
    }

    /**
     * 查询tips
     * @param model
     * @param userEmail
     */
    @Override
    public void showTips(Model model, String userEmail) {
        searchMessageStatus(userEmail,model);
    }

    @Override
    public Boolean checkEmail(String email, String userEmail) {
        User user = userMapper.selectByEmail(email);
        if(user == null){
            throw new GlobalException(CodeMsg.PersonalCodeMsg.USER_EMAIL_NON);
        }
        if(email.equals(userEmail)){
            throw new GlobalException(CodeMsg.PersonalCodeMsg.EMAIL_IS_YOUR);
        }
        return true;
    }



    @Override
    public boolean sendMessage(Message message, String userEmail) {
        message.setMsgReaded((byte) 0);
        message.setMsgTime(new Date());
        message.setMsgStatus(message.getMsgStatus());
        message.setMsgSenderEmail(userEmail);
        int succ = messageMapper.insertSelective(message);
        if (succ > 0) {
            return true;
        }
        return false;
    }

    /**
     * 更新
     * @param msgId
     * @return
     */
    @Override
    public int update(Message msgId) {
        int res = messageMapper.updateByPrimaryKeySelective(msgId);
        return res;
    }

    @Override
    public Message showOneMessage(Integer msgId, String userEmail, Model model) {
        searchMessageStatus(userEmail,model);
        return messageMapper.selectByPrimaryKey(msgId);
    }

    @Override
    public int del(Message message) {
        return messageMapper.deleteByPrimaryKey(message.getMsgId());
    }

    private void searchMessageStatus(String userEmail,Model model){
        // 查询未读
        Integer unreads = messageMapper.selectUnreadMessages(userEmail,(byte) 0);
        Integer draft = messageMapper.selectDraft(userEmail);
        Integer garbage = messageMapper.selectGarbage(userEmail);
        model.addAttribute("unreads",unreads);
        model.addAttribute("draft",draft);
        model.addAttribute("garbage",garbage);
    }
}
